package com.weita.lightfood.controller;


import com.weita.lightfood.model.entity.Address;
import com.weita.lightfood.model.entity.School;
import com.weita.lightfood.model.query.address.AddAddrQuery;
import com.weita.lightfood.model.query.address.UpdateAddrQuery;
import com.weita.lightfood.service.AddressService;
import com.weita.lightfood.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author IT_Monster at 2019/04/12 13:24.
 */
@RestController
@RequestMapping("/address")
@Api(tags = "用户地址接口")
public class AddressController {
    @Autowired
    private AddressService addressService;
    /**
     * 给用户添加地址
     * @param addAddrQuery
     * @return
     */
    @ApiOperation("用户添加地址")
    @ApiImplicitParams({@ApiImplicitParam(name = "schoolId",value = "学校id",defaultValue = "0"),
                        @ApiImplicitParam(name = "userId",value = "用户id",defaultValue = "0")
    })
    @PostMapping("/add")
    public Result<String> addAddress(AddAddrQuery addAddrQuery) {
        try {
            return Result.success(addressService.addAddress(addAddrQuery));
        } catch (Exception e) {
            System.out.println("用户添加地址出错");
            return Result.fail("添加地址失败");
        }
    }

    /**
     * 获取用户所有地址
     * @param userId
     * @return
     */
    @ApiOperation("获取用户所有地址")
    @PostMapping("/get")
    @ApiImplicitParam(name = "userId",value = "用户id",defaultValue = "45")
    public Result<List<Address>> getAddress(int userId) {
        try {
            return Result.success(addressService.getAddress(userId));
        } catch (Exception e) {
            System.out.println("获取用户地址时出错");
            return Result.fail("获取地址出错");
        }
    }

    /**
     * 修改用户地址
     * @param updateAddrQuery
     * @return
     */
    @ApiOperation("修改用户地址")
    @PostMapping("/update")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "schoolId", value = "学校id", defaultValue = "0"),
            @ApiImplicitParam(name = "addressId", value = "地址id", defaultValue = "29")
    })
    public Result<String> updateAddress(UpdateAddrQuery updateAddrQuery) {
        try {
            return Result.success(addressService.updateAddress(updateAddrQuery));
        } catch (Exception e) {
            System.out.println("修改用户地址时出错");
            return Result.fail("修改地址失败");
        }
    }

    /**
     * 删除地址
     * @param addressId
     * @return
     */
    @ApiOperation("删除地址")
    @PostMapping("/delete")
    @ApiImplicitParam(name = "addressId", value = "地址id", defaultValue = "0")
    public Result<String> deleteAddress(int addressId) {
        try {
            return Result.success(addressService.deleteAddress(addressId));
        } catch (Exception e) {
            return Result.fail("删除地址失败");
        }
    }

    /**
     * 获取学校信息接口
     * @return
     */
    @ApiOperation("获取学校信息接口")
    @PostMapping("/getSchools")

    public Result<List<School>> getSchools() {

        try {
            return Result.success(addressService.getSchools());

        } catch (Exception e) {
            return Result.fail("获取学校失败");
        }

    }
}
